package com.crazyxmq.symbol;

// 符号表
public class SymbolTable<Key,Value> {

    // 记录首结点
    private Node head;

    // 记录符号表中元素的个数
    private int N;

    private class Node {

        public Key key;

        public Value value;

        public Node next;

        public Node(Key key, Value value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }


    public SymbolTable() {
        this.head = new Node(null,null,null);
        this.N = 0;
    }

    public int size(){
        return N;
    }

    public void put(Key key,Value value){
        // 符号表中已经存在了 键为key的键值对，那么找到该结点，替换值为value即可
        Node n = head;
        while (n.next!=null){
            n = n.next;
            if(n.key.equals(key)){
                n.value = value;
                return;
            }
        }

        Node newNode = new Node(key, value, null);
        Node oldFirst = head.next;
        newNode.next = oldFirst;
        head.next = newNode;

        N++;

    }
    //
    public void delete(Key key){
        Node n = head;
        while (n.next!=null){
            if(n.next.key.equals(key)){
                n.next = n.next.next;
                N--;
                return;
            }
            n = n.next;
        }
    }

    public Value get(Key key){
        Node n = head;
        while (n.next!=null){
            n = n.next;
            if(n.key.equals(key)){
                return n.value;
            }
        }
        return null;
    }
}
